package stanaGUI;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import java.sql.*;

import javax.swing.JOptionPane;
public class LoginForm extends JFrame implements ActionListener {

	private Container container;
	private GridBagLayout layout;
	private GridBagConstraints gbc;
	private JButton cmdLogin, cmdCancel;
	private JLabel lblUSer, lblPassword;
	private JTextField txtUser;
	private JPasswordField txtPassword;
	private JTextField serverName;
	private JTextField dbName;
	static Connection con;
	MainWindow mainT;

	public LoginForm(MainWindow frame)
	{
		this.mainT = frame;
		setTitle("Login Screen"); // or  //super("Login window");
		setExtendedState(JFrame.MAXIMIZED_BOTH);
		setResizable(false); //disable resizing and Max button
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		setSize(300,350);
		setLocationRelativeTo(null);
		container = getContentPane();
		layout = new GridBagLayout();
		container.setLayout(layout);
		gbc = new GridBagConstraints();
		lblUSer = new JLabel("Username:");
		gbc.insets = new Insets(2,2,2,2);
		container.add(lblUSer, gbc);
		txtUser = new JTextField(15);
		gbc.gridx = 1;
		gbc.gridwidth = 3;
		container.add(txtUser, gbc);
		lblPassword = new JLabel("Password:");
		gbc.gridy = 1;
		gbc.gridx = 0;
		gbc.gridwidth = 1;
		container.add(lblPassword, gbc);
		txtPassword = new JPasswordField(15);
		gbc.gridx = 1;
		gbc.gridwidth = 3;
		container.add(txtPassword, gbc);

		JLabel lblServer = new JLabel("Server:");
		container.add(lblServer, gbc);
		gbc.gridy = 2;
		gbc.gridx = 0;
		gbc.gridwidth = 1;
		container.add(lblServer, gbc);
		
		serverName = new JTextField(15);
		gbc.gridx = 1;
		gbc.gridwidth = 3;
		container.add(serverName, gbc);

		
		JLabel lbldbName = new JLabel("Database Name:");
		container.add(lbldbName, gbc);
		gbc.gridy = 3;
		gbc.gridx = 0;
		gbc.gridwidth = 1;
		container.add(lbldbName, gbc);
		
		dbName = new JTextField(15);
		gbc.gridx = 1;
		gbc.gridwidth = 3;
		container.add(dbName, gbc);
		
		
		cmdLogin = new JButton("Login");
		cmdLogin.addActionListener( this );
		gbc.gridy = 4;
		gbc.gridx = 1;
		gbc.gridwidth = 1;
		container.add(cmdLogin, gbc);
		cmdCancel = new JButton("Cancel");
		cmdCancel.addActionListener( this );
		gbc.gridx = 2;
		container.add(cmdCancel, gbc);			
		setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		
		if(e.getActionCommand().equals("Login")) {
			connect(txtUser.getText().trim(), 
					txtPassword.getText().trim(), 
					serverName.getText().trim(), 
					dbName.getText().trim());
			if (con != null) {
				this.dispose();
				this.mainT = mainT;
				mainT.setVisible(true);
				DBBrowsingOp browse = new DBBrowsingOp((MainWindow) mainT,con,dbName.getText().trim(),
						txtUser.getText().trim(),
						txtPassword.getText().trim(), 
						serverName.getText().trim());
			} else {
				JOptionPane.showMessageDialog(this, 
						(Object)"Db Login is wrong");

			}
		}

	}
	public Connection getConnection() {
		return con;
	}
	public Connection connect(String user, String password, String server, String dbname)	{
		try		{
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			String conServer = "jdbc:mysql://" + server + "/" + dbname;
			con = DriverManager.getConnection(
					conServer, user, password);
			System.out.println("The connection is established");
			
		} catch (Exception e) 	{
			System.err.println ("Cannot connect to database server");
		} finally {
			
		}
		return con;
	} //connect()	
	
	public static void main(String args[]) {
			MainWindow frame = new MainWindow();
			new LoginForm(frame).setVisible(true);
		} //main()
}
